package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AbaPerfil extends JPanel {

	private static final long serialVersionUID = 1L;

	JLabel labelTextoNome;
	JLabel labelTextoLogin;
	JLabel labelTextoCPF;
	JLabel labelTextoEmail;
	JLabel labelTextoTelefone;
	JLabel labelTextoEndereco;
	JLabel labelTextoRua;
	JLabel labelTextoNumero;
	JLabel labelTextoCEP;
	JLabel labelTextoComplemento;
	JLabel labelTextoSaldo;

	JLabel labelNome;
	JLabel labelLogin;
	JLabel labelCPF;
	JLabel labelEmail;
	JLabel labelTelefone;
	JLabel labelRua;
	JLabel labelNumero;
	JLabel labelCEP;
	JLabel labelComplemento;
	JLabel labelSaldo;

	JButton buttonAlterar;
	JButton buttonLogout;

	JPanel painelSuperior;
	JPanel painelInferior;

	JPanel painelNomeLogin;
	JPanel painelCPFTelefone;
	JPanel painelEmail;
	JPanel painelEndereco;
	JPanel painelRua;
	JPanel painelNumeroCEP;
	JPanel painelComplemento;
	JPanel painelSaldo;

	public AbaPerfil() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		labelTextoNome = new JLabel("Nome: ");
		labelTextoLogin = new JLabel("Login: ");
		labelTextoCPF = new JLabel("CPF: ");
		labelTextoEmail = new JLabel("E-mail: ");
		labelTextoTelefone = new JLabel("Telefone: ");
		labelTextoEndereco = new JLabel("Endereco: ");
		labelTextoRua = new JLabel("Rua: ");
		labelTextoNumero = new JLabel("Numero: ");
		labelTextoCEP = new JLabel("CEP: ");
		labelTextoComplemento = new JLabel("Complemento: ");
		labelTextoSaldo = new JLabel("Saldo atual: R$");

		labelNome = new JLabel();
		labelLogin = new JLabel();
		labelCPF = new JLabel();
		labelEmail = new JLabel();
		labelTelefone = new JLabel();
		labelRua = new JLabel();
		labelNumero = new JLabel();
		labelCEP = new JLabel();
		labelComplemento = new JLabel();
		labelSaldo = new JLabel();

		buttonAlterar = new JButton("Alterar");
		buttonAlterar.addActionListener(new Ouvinte());
		buttonLogout = new JButton("Logout");
		buttonLogout.addActionListener(new Ouvinte());

		painelNomeLogin = new JPanel();
		painelNomeLogin.add(labelTextoNome);
		painelNomeLogin.add(labelNome);
		painelNomeLogin.add(labelTextoLogin);
		painelNomeLogin.add(labelLogin);

		painelCPFTelefone = new JPanel();
		painelCPFTelefone.add(labelTextoCPF);
		painelCPFTelefone.add(labelCPF);
		painelCPFTelefone.add(labelTextoTelefone);
		painelCPFTelefone.add(labelTelefone);

		painelEmail = new JPanel();
		painelEmail.add(labelTextoEmail);
		painelEmail.add(labelEmail);

		painelEndereco = new JPanel();
		painelEndereco.add(labelTextoEndereco);

		painelRua = new JPanel();
		painelRua.add(labelTextoRua);
		painelRua.add(labelRua);

		painelNumeroCEP = new JPanel();
		painelNumeroCEP.add(labelTextoNumero);
		painelNumeroCEP.add(labelNumero);
		painelNumeroCEP.add(labelTextoCEP);
		painelNumeroCEP.add(labelCEP);

		painelComplemento = new JPanel();
		painelComplemento.add(labelTextoComplemento);
		painelComplemento.add(labelComplemento);

		painelSaldo = new JPanel();
		painelSaldo.add(labelTextoSaldo);
		painelSaldo.add(labelSaldo);

		painelSuperior = new JPanel();
		painelSuperior
				.setLayout(new BoxLayout(painelSuperior, BoxLayout.Y_AXIS));
		painelSuperior.add(painelNomeLogin);
		painelSuperior.add(painelCPFTelefone);
		painelSuperior.add(painelEmail);
		painelSuperior.add(painelEndereco);
		painelSuperior.add(painelRua);
		painelSuperior.add(painelNumeroCEP);
		painelSuperior.add(painelComplemento);
		painelSuperior.add(painelSaldo);

		painelInferior = new JPanel();
		painelInferior.add(buttonAlterar);
		painelInferior.add(buttonLogout);

		this.add(painelSuperior);
		this.add(painelInferior);

	}

	public void update() {
		labelCEP.setText(Principal.cliente.getEnderecoCep());
		labelComplemento.setText(Principal.cliente.getEnderecoAdicional());
		labelCPF.setText(Principal.cliente.getCpf());
		labelEmail.setText(Principal.cliente.getEmail());
		labelRua.setText(Principal.cliente.getEnderecoRua());
		labelLogin.setText(Principal.cliente.getLogin());
		labelNome.setText(Principal.cliente.getNome());
		labelNumero.setText(Principal.cliente.getEnderecoNumero());
		labelTelefone.setText(Principal.cliente.getTelefone());
		labelSaldo.setText(String.valueOf(Principal.cliente.getCredito()));
	}

	class Ouvinte implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub

			if (arg0.getSource() == buttonAlterar) {
				Principal.tabManager.setEnabledAt("Perfil", false);
				Principal.tabManager.setEnabledAt("Editar Perfil", true);
				Principal.tabManager.setSelectedTab("Editar Perfil");

			} else { // acao veio de logout
				try {
					Principal.servidor
							.esvaziarCarrinho(Principal.itensCarrinho);
				} catch (RemoteException e) {
					e.printStackTrace();
				}

				Principal.tabManager.setEnabledAt("Perfil", false);
				Principal.tabManager.setEnabledAt("Carrinho", false);
				Principal.tabManager.setEnabledAt("Informacoes", false);
				Principal.tabManager.setEnabledAt("Login", true);
				Principal.tabManager.setSelectedTab("Login");
			}
		}
	}

}
